//
//  HLSyndromeTableViewController.m
//  NewKY
//
//  Created by 罗浩林 on 16/11/15.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "HLSyndromeTableViewController.h"
#import "HLSyndromeModel.h"

#import <SVProgressHUD.h>

@interface HLSyndromeTableViewController ()

@property (nonatomic,strong) NSArray *dataArr;

@property (nonatomic,strong) NSMutableArray *blockDataArr;

@property (nonatomic,assign) BOOL select;

@end

@implementation HLSyndromeTableViewController


-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self postMethod];
    
    self.blockDataArr = [NSMutableArray new];
    
    self.tableView.allowsMultipleSelectionDuringEditing=YES;
    [self.tableView setEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"syndromeCellID"];
    
    UIBarButtonItem * barBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(clickSyndromeBtn:)];
    
    self.navigationItem.rightBarButtonItem = barBtn;
    
    
}


- (void)postMethod{
    
    //选择并发症列表接口
    NSString *url = @"http://iosapi.itcast.cn/doctor/complicationList.json.php";
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@(self.ci1_id),@"ci2_id":@(self.ci2_id)};
    
    NSLog(@"%zd",self.ci2_id);
    
    [self postURL:url parameter:parameters];
    
}

- (void)postURL:(NSString *)url parameter:(NSDictionary *)parameters{
    
    [NetWorkingManager sendPOSTDataWithPath:url withParamters:parameters withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
        if(isSuccess){
            
            NSArray * dictArr = responseObject[@"data"];
            
            NSArray * modelArr = [NSArray modelArrayWithClass:[HLSyndromeModel class] json:dictArr];
            
            self.dataArr = modelArr;
            
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"syndromeCellID" forIndexPath:indexPath];
    
    HLSyndromeModel * model = self.dataArr[indexPath.row];
    
    cell.textLabel.text = model.complication_name;
    
    
    return cell;
}

//- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    NSIndexPath *oldIndex = [tableView indexPathForSelectedRow];
//    
//    [tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
//    
//    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
//    
//    return indexPath;
//    
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    HLSyndromeModel * model = self.dataArr[indexPath.row];
    
    [self.blockDataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([model.complication_name isEqualToString:obj] ) {
            
            [self.blockDataArr removeObject:obj];
        }
    }];
        
    [self.blockDataArr addObject:model.complication_name];
    
    NSLog(@"%@",model.complication_name);
    
}

-(void)clickSyndromeBtn:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if (self.dataArrBlock) {
        
        self.dataArrBlock(self.blockDataArr,YES);
        
    }
    
}


@end
